<?php

# Import the application settings.
require_once(dirname(__FILE__) . '/config.php');

# Import the products.
include(APPLICATION_ROOT . '/items.php');

$i = 0;
if (BSDDS_PLAYTAGGER) {
	print <<<PLAYTAGGER
		<script type="text/javascript" src="http://del.icio.us/js/playtagger.js"></script>
PLAYTAGGER;
}
?>

		<script type="text/javascript">
			function setPrice(id, a) {
				var sel = document.getElementById(id + "_price");
				if(sel.options) {
					var price = sel.options[sel.selectedIndex].value;
				}
				else {
					var price = sel.value;
				}
				a.href = a.href + "&price=" + price;
				//alert('setting price');
				//alert(a.href);
			}
		</script>
		<table border=0 width=100% cellspacing=0 cellpadding=4 summary='Catalog'>
<?php
foreach ($products as $product) {
	$which = NULL;
	if ($i/2 == round($i/2)) {
		$which = 'style="background-color: #EBF3FF;"';
	}

	$freefile = NULL;
	if ($product[freefile]) {
		$freefile = "<a style=\"text-decoration: none;\" href=\"" . $product[freefile] . "\"> </a>";
	}

	# Thumbnails.
	$thumbnail = NULL;

	if ($product[thumbnail]) {
		$thumbnail = '<img alt="Product thumbnail" src="' . $product[thumbnail] . '" border="0">';
	}
	if ($product[thumbnail_link]) {
		$thumbnail = '<a href="' . $product[thumbnail_link] . '">' . htmlentities($thumbnail) . '</a>';
	}

	$thumbnail = '<td>' . $thumbnail . '</td>';

	$formats = split(",", $product[formats]);
	$prices = split(",", $product[price]);

	$defaultPrice = $prices[0];

	$formatHTML = NULL;
	$c = 1;
	foreach ($formats as $format) {
		$format = trim($format);
		$nbspformat = str_replace(" ", "&nbsp;", htmlentities($format));
		if (sizeof($prices) > 1 || $prices[0] < 0.01) {
			$formatHTML .= "<a onclick=\"setPrice('" .
					urlencode($product[id]) .
					"', this); return true;\" href=\"" .
					WEB_ROOT . "go.php?id=" .
					urlencode($product[id]) .
					"&f=" . urlencode($format) .
					"\" target=\"_self\">$nbspformat</a>";
		} else {
			$formatHTML .= "<a href=\"" . WEB_ROOT .
					"go.php?id=" .
					urlencode($product[id]) .
					"&f=" . urlencode($format) .
					"&price=" . urlencode($prices[0]) .
					"\" target=\"_self\">$nbspformat</a>";
		}

		if ($c<sizeof($formats)) {
			$formatHTML .= "&nbsp;|&nbsp;";
			$c++;
		}
	}

	$priceHTML = NULL;

	# print_r($prices);

	if ($prices[0] == "any") {
		$priceHTML = "$<input type=\"\" size=\"5\" id=\"" .
				htmlentities($product[id]) . "_price\" />";
	} else if (sizeof($prices) > 1) {
		$priceHTML = "$<select id=\"" .
				htmlentities($product[id]) . "_price\">";
		sort($prices);

		foreach ($prices as $price) {
			$select = NULL;
			if ($price == $defaultPrice) {
				$select = "SELECTED";
			}

			$priceHTML .= "<option value=\"" . htmlentities($price) .
					"\" $select>" . htmlentities($price) .
					"</option>";
		}

		$priceHTML .= "</select>";
	} else {
		$priceHTML = "$" . htmlentities($prices[0]);
	}

	$i++;
	$product_name = htmlentities($product[name]);
	$product_type = htmlentities($product[type]);
	print <<<ITEM
			<tr $which>
				$thumbnail
				<td valign="middle" width="100%">
					$freefile
					<strong>{$product_name}</strong> ($product_type)
				</td>
				<td align="center" style="white-space: nowrap">
					$priceHTML
				</td>
				<td align="left" style="white-space: nowrap">
					$formatHTML
				</td>
			</tr>
ITEM;

}

?>

		</table>
		<p align="right">
			<?php echo '<a href="' . WEB_ROOT . 'go.php?c=cart" target="_self">' . "\n"; ?>
			<img alt="View Cart" border="0" src="https://www.paypal.com/en_US/i/btn/view_cart.gif"></a>
		</p>